﻿using Framework.WebApi.UnifyResult.Providers;

namespace Framework.WebApi.UnifyResult
{
	/// <summary>
	/// 统一结果规范
	/// </summary>
	public static class UnifyResultExtension
	{
		/// <summary>
		/// 添加统一结果规范
		/// </summary>
		/// <param name="serviceCollection"></param>
		public static void AddUnifyResult(this IServiceCollection serviceCollection)
		{
			//成功规范接口
			serviceCollection.AddTransient<SucceededUnifyResultFilter>();
			//异常规范接口
			serviceCollection.AddTransient<FriendlyExceptionFilter>();
			serviceCollection.AddTransient<IUnifyResultProvider, RESTfulResultProvider>();
			
			serviceCollection.AddMvc(options =>
			{
				options.Filters.AddService<SucceededUnifyResultFilter>(99);
				options.Filters.AddService<FriendlyExceptionFilter>(100);

				// 添加应用模型转换器
				options.Conventions.Add(new ApiControllerApplicationModelConvention(serviceCollection));
			});
		}
	}
}
